<div class="container-xl">
  <!-- Page title -->
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title">
          自定义下载器
        </h2>
      </div>
    </div>
    <div class="col-auto ms-auto d-print-none">
        <div class="btn-list">
          <a href="javascript:show_add_downloader_modal()" class="btn btn-primary d-none d-sm-inline-block">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
            新增下载器
          </a>
          <a href="javascript:show_add_downloader_modal()" class="btn btn-primary d-sm-none btn-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
          </a>
        </div>
      </div>
  </div>
</div>
<!-- 业务页面代码 -->
<div class="page-body">
  <div class="container-xl">
    <div class="row row-cards">
      <div class="col-12">
        <div class="card">
          <div class="card-body border-bottom py-3">
            <div class="d-flex">
              <div class="text-muted">
                共 {{ Count }} 条记录
              </div>
            </div>
          </div>
          <div class="table-responsive">
            <table class="table table-vcenter card-table">
              <thead>
                <tr>
                  <th>名称</th>
                  <th>类型</th>
                  <th>地址</th>
                  <th class="w-3"></th>
                </tr>
              </thead>
              <tbody>
              {% if Downloaders %}
                {% for Downloader in Downloaders %}
                <tr>
                  <td>{{ Downloader[1] or '' }}</td>
                  <td>{{ Downloader[2] or '' }}</td>
                  <td>{{ Downloader[3]}}:{{ Downloader[4]}}</td>
                  <td>
                    <a href="javascript:delete_downloader('{{ Downloader[0] }}')" title="删除下载器">
                      <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-x" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                         <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                         <line x1="18" y1="6" x2="6" y2="18"></line>
                         <line x1="6" y1="6" x2="18" y2="18"></line>
                      </svg>
                    </a>
                  </td>
                </tr>
                {% endfor %}
              {% else %}
                <tr><td colspan="4" align="center">没有数据</td></tr>
              {% endif %}
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-add-downloader" tabindex="-1" role="dialog" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="card border-top-0">
        <div class="card-header">
          <h5 class="modal-title">新增下载器</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-lg-6">
              <div class="mb-3">
                <label class="form-label required">名称</label>
                <input type="text" value="" id="dl_name" class="form-control" placeholder="别名">
              </div>
            </div>
            <div class="col-lg-6">
              <div class="mb-3">
                <label class="form-label required">类型</label>
                <select class="form-select" id="dl_type">
                  <option value="qbittorrent">Qbittorrent</option>
                  <option value="transmission">Transmission</option>
                </select>
              </div>
            </div>
            <div class="col-lg-6">
              <div class="mb-3">
                <label class="form-label required">IP地址或域名 <span class="form-help bg-red-lt" title="配置IP地址或域名，如为https则需要增加https://前缀" data-bs-toggle="tooltip">?</span></label>
                <input type="text" value="" id="dl_host" class="form-control" placeholder="ip/http/https">
              </div>
            </div>
            <div class="col-lg-6">
              <div class="mb-3">
                <label class="form-label required">端口</label>
                <input type="text" value="" id="dl_port" class="form-control">
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-lg-6">
              <div class="mb-3">
                <label class="form-label required">用户名</label>
                <input type="text" value="" id="dl_username" class="form-control">
              </div>
            </div>
            <div class="col-lg-6">
              <div class="mb-3">
                <label class="form-label required">密码</label>
                <input type="password" value="" id="dl_password" class="form-control">
              </div>
            </div>
          </div>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="mb-3">
              <label class="form-label required">保存目录 <span class="form-help bg-red-lt" title="下载器使用的文件保存目录" data-bs-toggle="tooltip">?</span></label>
              <input type="text" value="" id="dl_save_dir" class="form-control" placeholder="下载器使用路径">
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <a href="javascript:test_downloader_config()" id="dl_test_btn" class="btn btn-white me-auto">
          测试
        </a>
        <a href="javascript:add_downloader()" id="dl_save_btn" class="btn btn-primary">确定</a>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  // 打开新增窗口
  function show_add_downloader_modal(){
    $("#modal-add-downloader").modal('show');
  }

  // 新增下载器
  function add_downloader(test){
    if(!test){
      test = "";
    }
    var dl_name = $("#dl_name").val()
    if(!dl_name){
      $("#dl_name").addClass("is-invalid");
      return;
    } else {
      $("#dl_name").removeClass("is-invalid");
    }
    var dl_host = $("#dl_host").val()
    if(!dl_host){
      $("#dl_host").addClass("is-invalid");
      return;
    } else {
      $("#dl_host").removeClass("is-invalid");
    }
    var dl_port = $("#dl_port").val()
    if(!dl_port || isNaN(dl_port)){
      $("#dl_port").addClass("is-invalid");
      return;
    } else {
      $("#dl_port").removeClass("is-invalid");
    }
    var dl_save_dir = $("#dl_save_dir").val()
    if(!dl_save_dir){
      $("#dl_save_dir").addClass("is-invalid");
      return;
    } else {
      $("#dl_save_dir").removeClass("is-invalid");
    }
    var params = {test: test,
                  name: dl_name,
                  type: $("#dl_type").val(),
                  host: dl_host,
                  port: dl_port,
                  username: $("#dl_username").val(),
                  password: $("#dl_password").val(),
                  save_dir: dl_save_dir
    };
    ajax_post("add_downloader", params, function (ret) {
      if(test){
        if(ret.code == 0){
          $("#dl_test_btn").text("测试成功");
        }else{
          $("#dl_test_btn").text("测试失败！");
        }
      }else{
        $("#modal-add-downloader").modal('hide');
        navmenu('userdownloader');
      }
    });
  }

  // 删除下载器
  function delete_downloader(id){
    var params = {"id": id};
    ajax_post("delete_downloader", params, function (ret) {
      navmenu('userdownloader');
    });
  }

  // 测试下载器
  function test_downloader_config(){
    add_downloader(true);
  }
</script>